package demo.d1;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ServiceFacetry implements InvocationHandler {

    private Object factory;

    public ServiceFacetry(Object factory) {
        this.factory = factory;
    }


    public <T> T getInstance(){
        return (T)Proxy.newProxyInstance(factory.getClass().getClassLoader(),factory.getClass().getInterfaces(),this);
    }



    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        before();
        Object obj = method.invoke(factory,args);
        after();
        return obj;
    }

    private void before(){
        System.out.println("start");
    }

    private void after(){
        System.out.println("after");
    }
}
